﻿package org.jecat.pusher {
	
	import flash.net.Socket;
	import flash.utils.* ;
	
	public class PackageReceive {

		public function PackageReceive()
		{
			Reset() ;
		}

		private function ReceiveByte(aSocket:Socket):int
		{
			if(aSocket.bytesAvailable<1)
			{
				return -1 ;
			}
			return aSocket.readUnsignedByte() ;
		}
		
		public function GetBufferLength()
		{
			return m_arrPackageBuffer.length ;
		}

		public function Receive(aSocket:Socket): Boolean
		{
			var byte: int = -1 ;
			
			// 读取 头字节标记
			if(m_nHeadByte1<0)
			{
				byte = -1 ;
				do{
					if( (byte=ReceiveByte(aSocket))<0 )
					{
						return false ;
					}
				}while( byte!=0x04 ) ;
				
				m_nHeadByte1 = byte ;
			}
						
			// 读取 包长度高位字节
			if(m_nHeadByte2<0)
			{
				byte = ReceiveByte(aSocket) ;
				if(byte<0)
				{
					return false ;
				}
				if(byte>255)
				{
					trace("无效的长度") ;
					Reset() ;
					return false ;
				}
				m_nHeadByte2 = byte ;
			}
						
			// 读取 包长度低位字节
			if(m_nHeadByte3<0)
			{
				byte = ReceiveByte(aSocket) ;
				if(byte<0)
				{
					return false ;
				}
				if(byte>255)
				{
					trace("无效的长度") ;
					Reset() ;
					return false ;
				}
				m_nHeadByte3 = byte ;
				
				// 长度
				m_nPackageLen = ((m_nHeadByte2&0xFF)<<8) | (m_nHeadByte3&0xFF) ;
			}
			
			// 读取 包长度低位字节
			if(m_nHeadByte4<0)
			{
				byte = ReceiveByte(aSocket) ;
				if(byte<0)
				{
					return false ;
				}
				if(byte>255)
				{
					trace("无效的长度") ;
					Reset() ;
					return false ;
				}
				m_nHeadByte4 = byte ;
				
				// 压缩
				m_bCompress = m_nHeadByte4==1? true: false ;
			}
			
			// 读取包内容
			if( aSocket.bytesAvailable>0 )
			{
				var nWaitingBytes = m_nPackageLen - GetBufferLength() ;
				if(nWaitingBytes>0)
				{
					var nReadBytes = (nWaitingBytes > aSocket.bytesAvailable)? aSocket.bytesAvailable: nWaitingBytes ;
					
					aSocket.readBytes(m_arrPackageBuffer,m_arrPackageBuffer.length,nReadBytes) ;
				}
			}
			
			// 检查 尾字节
			if(GetBufferLength() == m_nPackageLen)
			{
				byte = ReceiveByte(aSocket) ;
				if(byte<0)
				{
					return false ;
				}
				
				// 尾字节正确
				else if(byte==0x03)
				{
					return true ;
				}
				
				// 尾字节不正确
				else
				{
					// 丢弃此包的内容
					Reset() ;
					return false ;
				}
			}
			
			return false ;
		}
		
		public function Reset()
		{
			m_nHeadByte1 = -1 ;
			m_nHeadByte2 = -1 ;
			m_nHeadByte3 = -1 ;
			m_nHeadByte4 = -1 ;
			m_nPackageLen = 0 ;
			m_bCompress = false ;
			
			m_arrPackageBuffer.position = 0 ;
			m_arrPackageBuffer.clear() ;
		}
		
		public function GetContent() 
		{
			if(m_bCompress)
			{
				m_arrPackageBuffer.uncompress() ;
			}
			return m_arrPackageBuffer.toString() ;
		}
		
		public function GetLength() 
		{
			return m_nPackageLen ;
		}

		private var m_nHeadByte1:int = -1 ;
		private var m_nHeadByte2:int = -1 ;
		private var m_nHeadByte3:int = -1 ;
		private var m_nHeadByte4 = -1 ;
		private var m_nPackageLen:int = 0 ;
		private var m_bCompress = false ;
		private var m_arrPackageBuffer:ByteArray = new ByteArray() ;
	}
	
	
}
